#import "ToDoSetDateView.h"

@implementation ToDoSetDateView

- (id)initWithFrame:(struct CGRect)frame
{
	self = [super initWithFrame:frame];
	
	toDoManager = [ToDoManager sharedToDoManager];
	td = [MobileToDoListApplication sharedInstance];
	dateType = 0;
	
	struct CGRect rect = [UIHardware fullScreenApplicationContentRect];
	rect.origin.x = rect.origin.y = 0.0f;
	
	table = [[UIPreferencesTable alloc] initWithFrame:CGRectMake(0.0f, 45.0f, 320.0f, 420.0f)];
	[table setDataSource:self];
	[table setDelegate:self];
	[table reloadData];
	
	navBar = [[UINavigationBar alloc] init];
	[navBar setFrame:CGRectMake(rect.origin.x, rect.origin.y, rect.size.width, 45.0f)];
	[navBar showButtonsWithLeftTitle:@"back" rightTitle:nil leftBack:YES];
	[navBar setDelegate:self];
	[navBar enableAnimation];
	
	[self addSubview:navBar];
	[self addSubview:table];
	
	
	// Date Picker
	
	datePicker = [[UIDatePicker alloc] init];
	[datePicker setFrame:CGRectMake(rect.origin.x, 230.0f, rect.size.width, 230.0f)];
	[datePicker setDelegate:self];
	[self addSubview:datePicker];
	
	return self;
}

- (void)reloadTableData
{
	[table reloadData];
}
- (void)setDateType:(int)date {
	
	dateType = date;
	
	if(dateType == 0) {
		if([toDoManager getCreationDateFromToDo:[toDoManager getCurrentToDo]]) {
			[datePicker setDate:[toDoManager getCreationDateFromToDo:[toDoManager getCurrentToDo]] ];
		}
		
	}
	if(dateType == 1) {
		if([toDoManager getDueDateFromToDo:[toDoManager getCurrentToDo]]) {
			[datePicker setDate:[toDoManager getDueDateFromToDo:[toDoManager getCurrentToDo]] ];
		}
	}
	
}
#pragma mark ----------Delegate Methods-----------

- (void)navigationBar:(UINavigationBar*)bar buttonClicked:(int)button
{
	if(dateType == 0) {
		[toDoManager setCreationDate:[datePicker date] forToDo:[toDoManager getCurrentToDo]];
	}
	if(dateType == 1) {
		[toDoManager setDueDate:[datePicker date] forToDo:[toDoManager getCurrentToDo]];
	}
	[toDoManager writeToFile];
	
	//go back
	[table selectRow: 0 byExtendingSelection: NO withFade: NO];
	[td showToDoDetailsWithTransition:2];
}

- (void)tableRowSelected:(NSNotification *)notification
{
	if ([table selectedRow] == 1)
	{
		if(dateType == 0) {
			[toDoManager setCreationDate:nil forToDo:[toDoManager getCurrentToDo]];
		}
		if(dateType == 1) {
			[toDoManager setDueDate:nil forToDo:[toDoManager getCurrentToDo]];
		}
		[toDoManager writeToFile];
		
		//go back
		[table selectRow: 0 byExtendingSelection: NO withFade: NO];
		[td showToDoDetailsWithTransition:2];
	}
}

#pragma mark ----------Datasource Methods-----------

- (int)numberOfGroupsInPreferencesTable:(UIPreferencesTable *)table
{
	return 1;
}

- (int)preferencesTable:(UIPreferencesTable *)table numberOfRowsInGroup:(int)group
{
	return 1;
}

- (UIPreferencesTableCell *)preferencesTable:(UIPreferencesTable *)table cellForGroup:(int)group
{
	UIPreferencesTableCell *cell = [[UIPreferencesTableCell alloc] init];
	
	return [cell autorelease];
}

- (UIPreferencesTableCell *)preferencesTable:(UIPreferencesTable *)table cellForRow:(int)row inGroup:(int)group
{
	if (group == 0)
	{
		if (row == 0)
		{
			UIPreferencesTableCell *cell;
			cell = [[UIPreferencesTableCell alloc] init];
			//[[cell textField] setDelegate:self];
			[cell setTitle:@"Unset This Date"];
			return [cell autorelease];
		}
	}
}

@end